/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ModuloConfiguracion;

import Acceso.Security;
import Modelo.DAL.Rol_DAO;
import Modelo.Rol;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.io.PrintWriter;
import org.json.JSONObject;

/**
 *
 * @author Rafael
 */
public class listaRolesAction extends org.apache.struts.action.Action {

    private static final String[] ALLOWED = {"Administrador", "Gestionar la Configuracion del Sistema","Listar Roles"};
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        String nombreRol = request.getParameter("nombre");
        String categoria = request.getParameter("categoria");

        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        String asc = request.getParameter("asc");

        if (order == null || order.equals("")) {
            order = "categoria, id_rol";
        }

        Rol rol = new Rol();
        rol.setNombre(nombreRol);
        rol.setCategoria(categoria);

        response.setContentType("text/json");
        JSONObject data = Rol_DAO.consultarRoles(rol, limit, offset, order, asc);

        PrintWriter pw = response.getWriter();
        pw.write(data.toString());
        pw.flush();
        pw.close();

        return null;
    }
}
